<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 500px;
            height: 500px;
            border: 1px solid black;
            margin: 20px auto;
            border-radius: 20px;
            overflow: auto;
            background-color: rgb(229, 214, 193);
            padding: 20px;
            display: flex;
            flex-direction: column;
            position: relative;
        }

        .function {
            width: 500px;
            height: 40px;
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            margin: 0 auto;
        }

        #btn {
            width: 100px;
            text-align: center;
            font-size: 20px;
            line-height: 40px;
        }

        #txt {
            flex: 1;
            font-size: 20px;
            line-height: 40px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="text1"></div>
        <div class="text2"></div>

    </div>



    <div class="function">
        <textarea name="" id="txt"></textarea>
        <input type="button" value="发送" id="btn">
    </div>

</body>
<!-- 美化 模拟聊天对话框 案例的布局 -->
<script>
    var box = document.querySelector(".box")
    var txt = document.querySelector("#txt")
    var btn = document.querySelector("#btn")

    var i = 0;

    btn.onclick = function () {
        var p1 = document.createElement("p1")
        var p2 = document.createElement("p2")
        if (i == 0) {
            p1.innerHTML = txt.value;
            p1.style.backgroundColor = "gray"
            p1.style.borderRadius="10px"
            p1.style.padding = "10px"
            p1.style.width = "200px"
            p1.style.display = "block";
            box.appendChild(p1);
            txt.value = "";
            i = 1;
        } else {
            p2.innerHTML = txt.value;
            p2.style.backgroundColor = "rgb(179, 150, 216)"
            p2.style.borderRadius="10px"
            p2.style.padding = "10px"
            p2.style.width = "200px"
            p2.style.display = "block";
            box.appendChild(p2);
            txt.value = "";
            i = 0;
        }
    }

</script>

</html>